<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if (!is_null($model)) {

    foreach ($model as $m) {
        echo "<div class='user_active' id='" . $m->id . "'>";
        echo $m->user->fullname . '<br>';
        switch ($m->active_type) {
            case UserActive::LIKE_IMG :
                echo ' Like ';
                break;
            case UserActive::COMMENT_IMG :
                echo ' Comment ';
                break;
            default :
                break;
        }
        echo '<br/>';
        echo '<a href="' . Yii::app()->createUrl('image/view', array(
            'id' => $m->image->id
        )) . '">' . $m->image->title . '</a><br>';
        echo $m->active_at . '<br>';
        echo '<img width="150px" src="' . Yii::app()->baseUrl . '/upload/image/' . $m->image->img_url . '" /><br><hr>';
        ;
        echo "</div><br>";
    }
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">

    (function($) {
        NotifierSSE = (function() {
            var _src,
                    _handler = {
                onMessage: function(event) {
                    var json = eval('(' + event.data + ')');
                    var length = json.length;
                    for (var i = 0; i < length; i++) {
                        var sdata = "<div class='user_active' id='" + json[i].id + "'>";
                        sdata += json[i].user_fn + "<br/>";
                        switch (json[i].active_type) {
                            case "<?php echo UserActive::LIKE_IMG; ?>" :
                                sdata += " Like ";
                                break;
                            case "<?php echo UserActive::COMMENT_IMG ?>" :
                                sdata += ' Comment ';
                                break;
                            default :
                                break;
                        }
                        sdata += '<br/>';
                        sdata += '<a href="#">' + json[i].img_title + '</a><br>';
                        sdata += json[i].active_at + '<br>';
                        sdata += '<img width="150px" src=" <?php echo Yii::app()->baseUrl; ?>/upload/image/' + json[i].img_url + '" /><br><hr>';
                        ;
                        sdata += '</div>';
                        $(sdata).insertBefore('.user_active:first');
                    }
                }
            };
            return {
                init: function() {
                    _src = new EventSource("<?php echo Yii::app()->createUrl('site/notifymore'); ?>");
                    _src.addEventListener('message', _handler.onMessage, false);
                }
            };
        }());


// Document is ready
        $(document).bind('ready.app', function() {
            setTimeout(NotifierSSE.init, 40);
        });

    })(jQuery);

</script>